﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace HomeLibrary
{
    public partial class Form1 : Form
    {
        public Library Agent = new Library();
        public Form1()
        {
            InitializeComponent();
            
            
        }

        private void Form1_Load(object sender, EventArgs e)
        {
            try
            {
                Agent.LoadXmlData("LibraryType.xml");
                TreeNode library=treeView1.Nodes.Add("Library","Library");
                TreeNode n= library.Nodes.Add("Books","Books");
                foreach (BookType book in Agent.LibraryData.BooksList.Book)
                {
                    TreeNode node = new TreeNode();
                    node.Text = book.Title;
                    node.Tag = book;
                    n.Nodes.Add(node);
                }
                TreeNode authors = library.Nodes.Add("Authors", "Authors");
                foreach (AuthorType author in Agent.LibraryData.AuthorsList.Author)
                {
                    authors.Nodes.Add(author.DisplayName);
                }
               
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
                 
            
        }

        private void treeView1_NodeMouseClick(object sender, TreeNodeMouseClickEventArgs e)
        {
            
        }

        private void treeView1_AfterSelect(object sender, TreeViewEventArgs e)
        {
            //treeView1.SelectedNode = ((TreeView)sender).SelectedNode;
            //MessageBox.Show(treeView1.SelectedNode.ToString());
            try
            {
                ((ctrlBook)tabControl1.TabPages["detailsPage"].Controls["ctrlBook"]).Book = (BookType)treeView1.SelectedNode.Tag;
            }
            catch (Exception ex)
            {
                ;
            }
            ;
        }

        private void menuStrip1_ItemClicked(object sender, ToolStripItemClickedEventArgs e)
        {

        }

        private void saveToolStripMenuItem_Click(object sender, EventArgs e)
        {
            Agent.SaveXMLData("LibraryType.xml");
        }
    }
}
